<?php

/*
 * CopyRight  : (C)2012-2099 HaoTeam Inc.
 * Document   : Center.php
 * Created on : 2020-7-25 12:41:11
 * Author     : Tiger <1192851302@qq.com>
 * Description: This is NOT a freeware, use is subject to license terms.
 *              这即使是一个免费软件,使用时也请遵守许可证条款,得到当时人书面许可.
 *              未经书面许可,不得翻版,翻版必究;版权归属 HaoTeam Inc;
 */

namespace app\admin\controller;

use app\Request;
use haoteam\service\UtilService;
use haoteam\service\SessionService;
use haoteam\http\validates\AdminUserValidate;
use app\admin\model\AdminUser;

/**
 * 个人中心
 * Class Center
 * Tiger <1192851302@qq.com> 2020-7-25 12:41:11
 */
class Center {

    /**
     * 退出登录
     * @login true
     * @author Tiger <1192851302@qq.com>
     * @param Request $request
     * @return type
     */
    public function logout(Request $request) {
        return app('json')->successful(SessionService::instance()->logout($request->token()));
    }

    /**
     * 个人资料
     * @login true
     * @author Tiger <1192851302@qq.com>
     * @param Request $request
     * @return type
     */
    public function profile(Request $request) {
        return app('json')->successful($request->admin());
    }

    /**
     * 设置密码
     * @login true
     * @author Tiger <1192851302@qq.com>
     * @param Request $request
     * @return type
     */
    public function update_password(Request $request) {
        [$oldPassword, $password, $repassword] = UtilService::postMore([
                    'oldPassword',
                    'password',
                    'repassword'
                        ], $request, true);

        if ($request->admin()['password'] !== encrypt_password($oldPassword, $request->admin()['salt'])) {
            return app('json')->fail('原密码错误');
        }

        $validate = new AdminUserValidate();
        if ($validate->scene('forget')->check(compact('password', 'repassword')) == false) {
            return app('json')->fail($validate->getError());
        }

        try {
            AdminUser::update(['password' => encrypt_password($password, $request->admin()['salt'])], [
                'id' => $request->adminId()
            ]);
            SessionService::instance()->logout($request->token());
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }

        return app('json')->successful('设置成功');
    }

    /**
     * 设置资料
     * @login true
     * @author Tiger <1192851302@qq.com>
     * @param Request $request
     * @return type
     */
    public function update(Request $request) {
        $post = UtilService::postMore([
                    'real_name',
                    'email',
                    'avatar'
        ]);
        $post['username'] = $request->admin()['username'];
        $post['mobile'] = $request->admin()['mobile'];
        $post['id'] = $request->adminId();
        $validate = new AdminUserValidate();
        if ($validate->scene('update')->check($post) == false) {
            return app('json')->fail($validate->getError());
        }
        try {
            AdminUser::update($post, ['id' => $request->adminId()]);
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }
        return app('json')->successful('更新成功');
    }

}
